package com.ly.auth.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author luka.zhou
 * @date 2024-3-9 9:11
 * @description
 */
@Component
@Slf4j
public class AuthorizationRequestFilter extends GenericFilterBean {
    public static final ThreadLocal<String> threadLocalParam = new ThreadLocal();
    public static final ThreadLocal<String> threadLocalUrlParam = new ThreadLocal();
    public static final ThreadLocal<String> threadLanguageParam = new ThreadLocal();
    public static final ThreadLocal<String> threadSessionParam = new ThreadLocal();
    public static final ThreadLocal<String> curUser = new ThreadLocal<>();

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession();
        log.info("==============>>>请求uri:{},sessionid={}", request.getRequestURI(), session.getId());


        threadSessionParam.set(session.getId());
        chain.doFilter(req, res);
        //清空
        threadSessionParam.remove();
    }

}
